﻿<?php

/**

 * 前台登录 退出操作

 *

 *

 *

 *

 * @copyright  Copyright (c) 2007-2015 ShopNC Inc. (http://www.shopnc.net)

 * @license    http://www.shopnc.net

 * @link       http://www.shopnc.net

 * @since      File available since Release v1.1

 */







defined('ByShopWWI') or exit('Access Invalid!');



class loginControl extends mobileHomeControl {



    public function __construct(){

        parent::__construct();

    }



    /**

     * 登录

     */

    public function indexOp(){

        if(empty($_POST['username']) || empty($_POST['password']) || !in_array($_POST['client'], $this->client_type_array)) {

            output_error('登录失败');

        }
		if($_POST['username']=='许金龙'){
			output_error('禁止登录');
			exit;
		}
		$sql = "select * from shopwwi_member where member_name = '".$_POST['username']."'";
		$query = mysql_query($sql);
		if($query){
			$rs = mysql_fetch_array($query);
			if($rs['member_state']==0){
				output_error('禁止登录');
				exit;
			}
		}
        $model_member = Model('member');



        $array = array();

        $array['member_name']   = $_POST['username'];

        $array['member_passwd'] = md5($_POST['password']);

        $member_info = $model_member->getMemberInfo($array);

        if(empty($member_info) && preg_match('/^0?(13|15|17|18|14)[0-9]{9}$/i', $_POST['username'])) {//根据会员名没找到时查手机号

            $array = array();

            $array['member_mobile']   = $_POST['username'];

            $array['member_passwd'] = md5($_POST['password']);

            $member_info = $model_member->getMemberInfo($array);

        }

        if(empty($member_info) && (strpos($_POST['username'], '@') > 0)) {//按邮箱和密码查询会员

            $array = array();

            $array['member_name']   = $_POST['username'];

            $array['member_passwd'] = md5($_POST['password']);

            $member_info = $model_member->getMemberInfo($array);

        }



        if(!empty($member_info)) {

            $token = $this->_get_token($member_info['member_id'], $member_info['member_name'], $_POST['client']);

            if($token) {

                output_data(array('username' => $member_info['member_name'], 'userid' => $member_info['member_id'], 'key' => $token));

            } else {

                output_error('登录失败');

            }

        } else {

            output_error('用户名密码错误');

        }

    }



    /**

     * 登录生成token

     */

    private function _get_token($member_id, $member_name, $client) {

        $model_mb_user_token = Model('mb_user_token');



        //重新登录后以前的令牌失效

        //暂时停用

        //$condition = array();

        //$condition['member_id'] = $member_id;

        //$condition['client_type'] = $client;

        //$model_mb_user_token->delMbUserToken($condition);



        //生成新的token

        $mb_user_token_info = array();

        $token = md5($member_name . strval(TIMESTAMP) . strval(rand(0,999999)));

        $mb_user_token_info['member_id'] = $member_id;

        $mb_user_token_info['member_name'] = $member_name;

        $mb_user_token_info['token'] = $token;

        $mb_user_token_info['login_time'] = TIMESTAMP;

        $mb_user_token_info['client_type'] = $client;



        $result = $model_mb_user_token->addMbUserToken($mb_user_token_info);



        if($result) {

            return $token;

        } else {

            return null;

        }



    }



    /**

     * 注册

     */

    public function registerOp(){

        $model_member   = Model('member');

		if(!empty($_POST['rec_id']))

		{

		    $rec_id = $_POST['rec_id'];

		    $member=$model_member->getMemberInfo(array('member_id'=>$rec_id));

			$invite_one = $rec_id;

			$invite_two = $member['invite_one'];

			$invite_three = $member['invite_two'];	

		

		}else{

		    $invite_one = 0;

			$invite_two = 0;

			$invite_three = 0;

		

		}

        $register_info = array();

        $register_info['username'] = $_POST['username'];

        $register_info['password'] = $_POST['password'];

        $register_info['password_confirm'] = $_POST['password_confirm'];

        $register_info['email'] = $_POST['email'];
		$register_info['invite_one'] = $invite_one;

		$register_info['invite_two'] = $invite_two;

		$register_info['invite_three'] = $invite_three;
		
		$jjsid=$_POST['jjs_member_id'];
        $uniq=$_POST['uni_qianbao'];
		
        empty($jjsid)?'':$register_info['jjs_member_id'] = $jjsid;
        empty($uniq)?'':$register_info['uni_qianbao'] = $uniq;

        $member_info = $model_member->register($register_info);

        if(!isset($member_info['error'])) {

            $token = $this->_get_token($member_info['member_id'], $member_info['member_name'], $_POST['client']);

            if($token) {

                output_data(array('username' => $member_info['member_name'], 'userid' => $member_info['member_id'], 'key' => $token));

            } else {

                output_error('注册失败');

            }

        } else {

            output_error($member_info['error']);

        }



    }

}

